using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.Common;

namespace TXLooker.WorkItemExtention.Core
{
    static class WorkItemExtentionGlobal
    {
        public static List<WIStoredQuery> ObjectViewUpdate = new List<WIStoredQuery>();

        public static void SaveColumnsOrder(WIStoredQuery query, Microsoft.Office.Interop.Outlook.View olView)
        {
            WorkItemStore store = query.GetStore();
            Dictionary<string, FieldDefinition> dictFieldDefination = new Dictionary<string, FieldDefinition>();
            foreach (FieldDefinition field in store.FieldDefinitions)
                dictFieldDefination.Add(field.Name, field);

            XmlViewParser xmlParser = new XmlViewParser(olView.XML);
            List<FieldDefinition> fieldDefinations = new List<FieldDefinition>();

            foreach (string column in xmlParser.Columns)
            {
                if (!dictFieldDefination.ContainsKey(column))
                    continue;
                fieldDefinations.Add(dictFieldDefination[column]);
            }
            if (fieldDefinations.Count != 0)
                (query.FolderView as OlWIQueryFolderView).Init(fieldDefinations);
        }

        public static Uri GetServer(WIObject wiObject)
        {
            if (wiObject != null)
            {
                try
                {
                    switch (wiObject.Type)
                    {
                        case WIObjectType.Server:
                        case WIObjectType.Project:
                        case WIObjectType.QueriesRoot:
                        case WIObjectType.QueriesGroup:
                        case WIObjectType.Query:
                            //WIProjectProvider provider = (WIProjectProvider)wiObject.GetType().InvokeMember("ProjectProvider", BindingFlags.GetProperty, null, wiObject, null);
                            //WorkItemStore store = provider.GetStore();
                            //return (wiObject as WIServerProvider).GetServer().Uri;
                            return wiObject.GetStore().TeamFoundationServer.Uri;
                            //return store.TeamFoundationServer.Uri;
                        default:
                            throw new NotImplementedException();
                    }
                }
                catch (Exception ex)
                {
                    LogManager.WriteMessageToCustomLog("Error in GetServer " + ex.ToString());
                }
            }
            return null;
        }

        public static Project GetProject(WIObject item)
        {
            Project project = null;

            if (item != null)
            {
                try
                {
                    switch (item.Type)
                    {
                        case WIObjectType.Server:
                            break;
                        case WIObjectType.Project:
                            project = (item as WIProjectProvider).GetProject();
                            break;
                        case WIObjectType.QueriesRoot:
                        case WIObjectType.QueriesGroup:
                        case WIObjectType.Query:
                            WIProjectProvider provider = (WIProjectProvider)item.GetType().InvokeMember("ProjectProvider", BindingFlags.GetProperty, null, item, null);
                            project = provider.GetProject();
                            break;
                        default:
                            throw new NotImplementedException();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
            }

            return project;
        }

        public static StoredQueryCollection FindStoredQueries(WIStoredQuery wiQuery)
        {
            StoredQueryCollection storedQueries = null;

            try
            {
                Project project = GetProject(wiQuery);
                storedQueries = project.StoredQueries;
                storedQueries.Refresh();
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            return storedQueries;
        }

        public static string LookerTempFolder
        {
            get
            {   string path = System.IO.Path.Combine(Environment.CurrentDirectory, "LookerTemp");
                if(!System.IO.Directory.Exists(path))
                    System.IO.Directory.CreateDirectory(path);
                return path;
            }
        }

        public static string GetContentPatterFile(string folderPath,Uri serverUri)
        {
            return Path.Combine(folderPath,"TeamXpandContentPattern_" + serverUri.Host + ".xml");
        }

        private static List<Microsoft.Office.Interop.Outlook.MAPIFolder> m_queriesRootFolders = new List<Microsoft.Office.Interop.Outlook.MAPIFolder>();
        public static List<Microsoft.Office.Interop.Outlook.MAPIFolder> QueriesRootFolders
        {
            get { return m_queriesRootFolders; }
        }

        private static List<Microsoft.Office.Interop.Outlook.MAPIFolder> m_queriesGroupFolders = new List<Microsoft.Office.Interop.Outlook.MAPIFolder>();
        public static List<Microsoft.Office.Interop.Outlook.MAPIFolder> QueriesGroupFolders
        {
            get { return m_queriesGroupFolders; } 
        }

        private static List<Microsoft.Office.Interop.Outlook.MAPIFolder> m_queriesFolders = new List<Microsoft.Office.Interop.Outlook.MAPIFolder>();
        public static List<Microsoft.Office.Interop.Outlook.MAPIFolder> QueriesFolders
        {
            get { return m_queriesFolders; }
        }

        private static WorkItemExtentionDataProvider m_extentionDataProvider;
        public static WorkItemExtentionDataProvider ExtentionDataProvider
        {
            get 
            {
                if (m_extentionDataProvider == null)
                    m_extentionDataProvider = new WorkItemExtentionDataProvider();
                return m_extentionDataProvider;
            }
        }
    }
}
